package com.ksy.oss;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.ksy.common.utils.DateUtil;
import com.ksy.common.utils.LogTool;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import org.json.JSONObject;

import java.io.File;
import java.util.UUID;

public class UpdateFileRunnable implements Runnable {
    private UpdateListener listener;
    private OssClient.FileType fileType;
    private String srcPath;
    private String currentDate;
    private int tag;
    private final UploadManager upper;

    private boolean isServerUrl(String srcFilePath) {
        return srcFilePath.toLowerCase().startsWith("http") || srcFilePath.startsWith("https");
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public int getTag() {
        return tag;
    }

    public interface UpdateListener {
        void onProgress(UpdateFileRunnable runnable, String bucketName, String objectKey, String uploadFilePath, long currentSize, long totalSize);

        void onFinish(UpdateFileRunnable runnable, OssClient.Status status, OssClient.FileType fileType, String ossPath, String filePath);
    }

    public UpdateFileRunnable(Context context, @NonNull OssClient.FileType FileType, String srcPath, UpdateListener listener) {
        this.listener = listener;
        currentDate = DateUtil.getCurrentDate(DateUtil.Format_yyyyMMdd2);
        upper = OssClient.getInstance().getUpper();
        this.fileType = FileType;
        this.srcPath = srcPath;
    }


    @Override
    public void run() {
        if (TextUtils.isEmpty(srcPath)) {
            if (listener != null) {
                listener.onFinish(UpdateFileRunnable.this, OssClient.Status.Skip, fileType, "", "");
            }
            return;
        }
        if (isServerUrl(srcPath)) {
            if (listener != null) {
                listener.onFinish(UpdateFileRunnable.this, OssClient.Status.Success, fileType, srcPath, srcPath);
            }
            return;
        }

        File file = new File(srcPath);
        if (!file.exists() || !file.canRead() || !file.isFile()) {
            if (listener != null) {
                listener.onFinish(UpdateFileRunnable.this, OssClient.Status.Error_File, fileType, "", srcPath);
            }
            return;
        }
        //http://panixgsjz.bkt.clouddn.com/src/photos/2018/06/21/37dc0137-fb13-4552-b396-994ec0e55e0a_splash_cache_img.png
        UUID uuid = UUID.randomUUID();
        String filePath = fileType.path + "/" + currentDate + "/" + uuid.toString() + "_" + file.getName();
        final String ossPath = "http://panixgsjz.bkt.clouddn.com/" + filePath;
        Auth auth = Auth.create("Ax6OZsE9cKwQmNMzdCNSeOfKGBfj7pWUXkrGOLxg", "AxDY8hElECB_UL1QWEDJjyTr4eDYiJ7PFlI1VbhP");
        String s = auth.uploadToken("oem-connect");
        upper.put(file.getAbsolutePath(), filePath, s, new UpCompletionHandler() {
            @Override
            public void complete(String key, ResponseInfo info, JSONObject response) {
                String s = info.toString();
                LogTool.e(s);
                if (listener != null) {
                    boolean ok = info.isOK();
                    long sent = info.sent;
                    long totalSize = info.totalSize;
                    if (ok) {
                        listener.onFinish(UpdateFileRunnable.this, OssClient.Status.Success, fileType, ossPath, srcPath);
                    } else {
                        listener.onFinish(UpdateFileRunnable.this, OssClient.Status.Error_Oss, fileType, "", srcPath);
                    }
                }
            }
        }, null);
    }


}
